package com.ruoyi.activiti.controller;

import com.ruoyi.activiti.domain.BizExpensesClaim;
import com.ruoyi.activiti.domain.BizLeave;
import com.ruoyi.activiti.domain.CheckTask;
import com.ruoyi.activiti.service.IBizExpensesService;
import com.ruoyi.activiti.service.IBizLeaveService;
import com.ruoyi.activiti.service.IProcessService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.ruoyi.common.core.domain.AjaxResult.error;
import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * 请假Controller
 *
 * @author 一只闲鹿
 */
@RestController
@RequestMapping("/expenses")
@AllArgsConstructor
public class BizExpensesController extends BaseController
{
    private IBizExpensesService bizExpensesService;
    private IProcessService processService;

    /**
     * 查询请假列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BizExpensesClaim bizLeave)
    {
        startPage();
        List<BizExpensesClaim> list = bizExpensesService.selectBizExpensesList(bizLeave);
        return getDataTable(list);
    }
    @PostMapping("/checkTask")
    public AjaxResult checkTask(@RequestBody CheckTask checkTask){
        bizExpensesService.checkTask(checkTask);
        return AjaxResult.success();
    }
    @PostMapping("/selectByInstanceId")
    public AjaxResult selectByInstanceId(@RequestBody CheckTask checkTask){
        CheckTask checkTask1 = bizExpensesService.selectByInstanceId(checkTask);
        return AjaxResult.success(checkTask1);
    }

    /**
     * 获取请假详细信息
     */
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bizExpensesService.selectBizLeaveById(id));
    }
    @GetMapping(value = "/getByInstanceId")
    public AjaxResult getByInstanceId(String id)
    {
        return AjaxResult.success(bizExpensesService.selectBizLeaveByInstanceId(id));
    }

    /**
     * 新增请假
     */
    @Log(title = "请假", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody BizExpensesClaim bizLeave)
    {
        return toAjax(bizExpensesService.insertBizLeave(bizLeave));
    }
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BizExpensesClaim bizLeave)
    {
        return toAjax(bizExpensesService.updateBizExpensesById(bizLeave));
    }

    /**
     * 提交申请
     */
    @Log(title = "请假业务for wln", businessType = BusinessType.UPDATE)
    @PostMapping( "/submitApplyNew/{id}")
    @ResponseBody
    public AjaxResult submitApplyNew(@PathVariable Long id) {
        try {
           bizExpensesService.submitApply(id);
        } catch (Exception e) {
            e.printStackTrace();
            return error("提交申请出错：" + e.getMessage());
        }
        return success();
    }
}
